/**
 * 表结构维护
 * @author miaoyl
 * @date 2018/01/04
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        table,
        table_where = {

        },
        cols = [[
            {type:'checkbox', fixed: 'left'},
            {field: 'TABLE_ID', width: 120, title: '表ID', fixed: 'left',sort: true, search:'default'}
            , {field: 'TABLE_NAME', width: 120, title: '表名称', sort: true, search:'default'}
            ,{field: 'TABLE_REMARK', width: 137, title: '备注'}
            ,{field: 'CREATE_BY_NAME', width: 137, title: '创建人'}
            ,{field: 'CREATE_DATE', width: 137, title: '创建时间'}/*,
            {
                width: 60,
                title: '',
                toolbar: '#script-columnssetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }*/
        ]],
        columnsSettingAction,
        moduleName = '表结构',
        dbFlag = false
    ;

    var Columns = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'columns',
                _searchs: [{
                    id: 'columns', tableId: 'sys_columns_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'table_edit', tpl: 'system/table_edit'
                },{
                    dom: 'table_del_test', tpl: 'system/table_del_test'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'sys_columns_table'),
                    url: g_ctxPath + 'sys/table/getTablelist',
                    exportsUrl: g_ctxPath + 'sys/table/getTablelist',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    id: 'sys_columns_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'], //, 'exports'
                    height: 'full',
                    cols: cols,
                    page: true,
                    where: table_where
                });

                /*layui.laytpl(app.getByid(doc, 'script-columnssetting-action').innerHTML).render({}, function (string) {
                    columnsSettingAction = string;
                });*/

                var tipsIndex = null;

                table.on('tool(sys_columns_table)', function (obj) {

                    layer.tips(columnsSettingAction, obj.td, {
                        tips: [3, '']
                        , time: 5000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'table_edit',
                                            moduleName: moduleName,
                                            tagData: {
                                                obj: obj
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: moduleName + '信息编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delTables(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(sys_columns_table)', function (obj) {
                    self._openTplFull({
                        tag: 'table_edit',
                        moduleName: moduleName,
                        tagData: {
                            obj: obj,
                            isDetail:true
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '信息编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(sys_columns_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            if(dbFlag){
                                self._openTplFull({
                                     tag: 'table_edit',
                                     moduleName: moduleName,
                                     tagData: {
                                        is_add: true
                                     },
                                     index: 0
                                 });

                            }else {
                                self._openTplFull({
                                    tag: 'table_del_test',
                                    moduleName: moduleName,
                                    tagData: {
                                        is_add: true,
                                        cb: function (flag) {
                                            dbFlag = flag;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '数据库验证',
                                        area: ['400px', '300px']
                                    }
                                });
                            }
                            break;
                        case 'edit':
                            /*self._openTplFull({
                                tag: 'table_edit',
                                moduleName: moduleName,
                                tagData: {
                                    obj: obj
                                },
                                index: 0,
                                openOpts:{
                                    title: moduleName + '信息编辑',
                                    maxmin: true
                                }
                            });*/
                            self.editData();
                            break;
                        case 'del':
                            self.deleteDataBatch();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'sys_columns_table', where: table_where});
        },

        delTables: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'sys/table/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName + '信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        editData: function () {
            var checkStatus = table.checkStatus('sys_columns_table');
            if (checkStatus.data.length > 0) {
                if (checkStatus.data.length > 1) {
                    app.showWarning("只允许对单笔数据进行操作！");
                    return;
                }else {
                    if(dbFlag) {
                        self._openTplFull({
                            tag: 'table_edit',
                            moduleName: moduleName,
                            tagData: {
                                obj: {data: checkStatus.data[0]}
                            },
                            index: 0,
                            openOpts: {
                                title: moduleName + '信息编辑',
                                maxmin: true
                            }
                        });
                    }else {
                        self._openTplFull({
                            tag: 'table_del_test',
                            moduleName: moduleName,
                            tagData: {
                                obj: {data: checkStatus.data[0]},
                                is_edit: true,
                                cb:function (flag) {
                                    dbFlag = flag;
                                }
                            },
                            index: 1,
                            openOpts: {
                                title: '数据库验证',
                                area: ['400px', '300px']
                            }
                        });
                    }
                }
            }else {
                app.showWarning("请先选择单据！");
                return;
            }
        },

        deleteDataBatch: function () {
            var checkStatus = table.checkStatus('sys_columns_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('sys_table_column');
                        _temp.TABLE_ID = v.TABLE_ID;
                        _xdata.push(_temp);
                });
            }else {
                app.showWarning("请先选择单据！");
                return;
            }
            if(dbFlag) {
                app.ajax({
                    xurl: 'sys/table/deleteDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm: {msg: "是否确认删除?"},
                    okmsg: moduleName + '信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }else {
                self._openTplFull({
                    tag: 'table_del_test',
                    moduleName: moduleName,
                    tagData: {
                        obj: _xdata,
                        is_del: true,
                        cb:function (flag) {
                            dbFlag = flag;
                        }
                    },
                    index: 1,
                    openOpts: {
                        title: '数据库验证',
                        area: ['400px', '300px']
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'columns',
                    methods: {}
                }]
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Columns();

});